<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Goals_model extends CI_Model
{
	
	public function __construct()
	{
		parent::__construct();
	} 	
	
	public function get_objectives()
	{
	return $this->db->get('objectives')->result();	
	}
	
	public function create_objective($insert)
	{
	$this->db->insert('objectives',$insert);	
	}
	
	public function create_goal($insert)
	{
	$this->db->insert('goals',$insert);	
	}
		
	public function get_goals()
	{
	return $this->db->get('goals')->result();	
	}
	
	public function get_my_goals()
	{
	return $this->db->get_where('goals',array('user_id'=>$this->session->userdata('user_id')))->result();	
	}
	
	 /* function of key results starts here */
	public function get_goals_by_id($id)
	{
		return $this->db->get_where('goals',array('objective_id'=>$id))->result();
	}
	
	public function get_goal_details($id)
	{
return $this->db->get_where('goals',array('id'=>$id))->row_array();
	}
	 public function update_result($update,$id)
	  {
		//  print_r($id);
		  //exit;
		$this->db->update('results',$update,array('id'=>$id));  
	  }
	public function delete_goal($id)
	  {
		  $this->db->delete('goals',array('id'=>$id));
	  }
	
	  /* function of key results starts here */
	  
	  public function get_results($goal_id)
	  {
return $this->db->order_by('is_achieved asc,ordering asc')->get_where('results',array('goal_id'=>$goal_id))->result();
	  }
	  
	  public function create_result($insert)
	  {
		$this->db->insert('results',$insert);
	  }
	  
	  public function edit_result($id)
	{
		return $this->db->get_where('results',array('id'=>$id))->result();
	}
	  
	  public function delete_result($id)
	  {
		  $this->db->delete('results',array('id'=>$id));
	  }
	  
	 public function result_soarting($action,$updateRecordsArray)
		{
			
		if ($action == "updateRecordsListings"){
		
			$listingCounter = 1;
			foreach ($updateRecordsArray as $recordIDValue) {
				
				$this->db->update('results',array('ordering'=>$listingCounter),array('id'=>$recordIDValue));		
				$listingCounter = $listingCounter + 1;		
			}			
		  } 
		}
		 
	  /* note function  */
	public function add_note($insert)
	{
		$this->db->insert('goal_notes',$insert);
	}
	
	public function get_note_by_id($id)
	{
		$this->db->select('goal_notes.*,users.username,users.profile_image');
		$this->db->from('goal_notes');
		$this->db->where('goal_notes.goal_id ='.$id);
		$this->db->join('users','goal_notes.user_id = users.id');
		
	 return $this->db->get()->result();	
	}
	
	public function delete_note($id)
	{		
		if($this->db->delete('goal_notes',array('id'=>$id)))
		{
		return true;	
		}
		else { return false ; }
	}
	 /* note function ends here */
	 public function create_result_subheading($insert)
	{
		$this->db->insert('results',$insert);
	}
	 
	 public function delete_subheading($id)
	{		
		if(!$this->db->delete('results',array('id'=>$id)))
		{
			return 'Some Error has Occured';
		}
	}

}
